package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private LogService logService;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> getListInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offset = (page -1)*rows;
        List<Goods> goodsList = goodsDao.getListInventory(offset,rows,codeOrName,goodsTypeId);
        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    @Override
    public Map<String, Object> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offset = (page -1)*rows;
        List<Goods> goodsList = goodsDao.getGoodsList(offset,rows,goodsName,goodsTypeId);
        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    @Override
    public ServiceVO save(Goods goods) {
        //判断是否存在，存在就更新
        if (goods.getGoodsId() == null){
            goodsDao.save(goods);
            logService.save(new Log(Log.INSERT_ACTION,"新增商品"+goods.getGoodsId()));

        }
        goodsDao.update(goods);
        logService.save(new Log(Log.INSERT_ACTION,"更新商品"+goods.getGoodsId()));

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);

    }

    @Override
    public ServiceVO delete(Integer goodsId) {
        //进行判断 （要判断商品状态,入库、有进货和销售单据的不能删除）
        //0表示初始值,1表示已入库，2表示有进货或销售单据
        Goods goods = goodsDao.getGoodsByGoodsId(goodsId);
        if (goods.getState()!= 0){
            return new ServiceVO(ErrorCode.GOODS_TYPE_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }
        goodsDao.delete(goodsId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);

    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page = page == 0 ? 1:page;
        int offset = (page -1)*rows;

        Map<String,Object> map = new HashMap<>();
        //分页
        List<Customer> customerList =  goodsDao.getNoInventoryQuantity(offset,rows,nameOrCode);
        //查找模糊查询的数量
        map.put("total",customerList.size());
        map.put("rows",customerList);
        logService.save(new Log(Log.SELECT_ACTION,"分页查询客户信息"));
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page = page == 0 ? 1:page;
        int offset = (page -1)*rows;

        Map<String,Object> map = new HashMap<>();
        //分页
        List<Customer> customerList =  goodsDao.getHasInventoryQuantity(offset,rows,nameOrCode);
        //查找模糊查询的数量
        map.put("total",customerList.size());
        map.put("rows",customerList);
        logService.save(new Log(Log.SELECT_ACTION,"分页查询客户信息"));
        return map;
    }

    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = goodsDao.getGoodsByGoodsId(goodsId);
       /* if (goods.getInventoryQuantity() <= 0){
            //添加库存
            goods.setInventoryQuantity(inventoryQuantity);
            goods.setPurchasingPrice(purchasingPrice);
        }*/
        goods.setInventoryQuantity(inventoryQuantity);
        goods.setPurchasingPrice(purchasingPrice);
        goodsDao.update(goods);
       // goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = goodsDao.getGoodsByGoodsId(goodsId);
        if (goods.getState() == 2){
            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);
        }
        goods.setInventoryQuantity(0);
        goodsDao.deleteStock(goodsId);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> listAlarm() {
        List<Goods> goodsList = goodsDao.listAlarm();
        Map<String,Object> map = new HashMap<>();
        map.put("rows",goodsList);
        return map;
    }


}
